Page({
  data: {
    regUsername: '',
    regPassword: '',
    loginUsername: '',
    loginPassword: '',
    updatePwUsername: '',
    updatePwOldPassword: '',
    updatePwNewPassword: '',
    updatePwByKeyUsername: '',
    updatePwByKeyKey: '',
    updatePwByKeyNewPassword: '',
    deleteUsername: '',
    deletePassword: ''
  },

  // 注册相关输入事件
  onRegUsernameInput: function (e) {
    this.setData({
      regUsername: e.detail.value
    });
  },
  onRegPasswordInput: function (e) {
    this.setData({
      regPassword: e.detail.value
    });
  },
  // 登录相关输入事件
  onLoginUsernameInput: function (e) {
    this.setData({
      loginUsername: e.detail.value
    });
  },
  onLoginPasswordInput: function (e) {
    this.setData({
      loginPassword: e.detail.value
    });
  },
  // 根据账号密码修改密码相关输入事件
  onUpdatePwUsernameInput: function (e) {
    this.setData({
      updatePwUsername: e.detail.value
    });
  },
  onUpdatePwOldPasswordInput: function (e) {
    this.setData({
      updatePwOldPassword: e.detail.value
    });
  },
  onUpdatePwNewPasswordInput: function (e) {
    this.setData({
      updatePwNewPassword: e.detail.value
    });
  },
  // 根据账号救援码修改密码相关输入事件
  onUpdatePwByKeyUsernameInput: function (e) {
    this.setData({
      updatePwByKeyUsername: e.detail.value
    });
  },
  onUpdatePwByKeyKeyInput: function (e) {
    this.setData({
      updatePwByKeyKey: e.detail.value
    });
  },
  onUpdatePwByKeyNewPasswordInput: function (e) {
    this.setData({
      updatePwByKeyNewPassword: e.detail.value
    });
  },
  // 删除用户相关输入事件
  onDeleteUsernameInput: function (e) {
    this.setData({
      deleteUsername: e.detail.value
    });
  },
  onDeletePasswordInput: function (e) {
    this.setData({
      deletePassword: e.detail.value
    });
  },

  // 注册接口调用
  signup: function () {
    const baseUrl = 'http://39.104.24.231:8082';
    const url = baseUrl + '/user/signup';
    const { regUsername, regPassword } = this.data;
    wx.request({
      url: url,
      method: 'POST',
      data: {
        USER_NAME: regUsername,
        USER_PASSWORD: regPassword
      },
      header: {
        'content-type': 'application/json'
      },
      success: (res) => {
        if (res.data.status === 200) {
          wx.showToast({
            title: '注册成功',
            icon: 'success'
          });
        } else {
          wx.showToast({
            title: '注册失败: ' + res.data.message,
            icon: 'none'
          });
        }
      },
      fail: (err) => {
        wx.showToast({
          title: '请求失败: ' + err.errMsg,
          icon: 'none'
        });
      }
    });
  },

  // 登录接口调用
  login: function () {
    const baseUrl = 'http://39.104.24.231:8082';
    const url = baseUrl + '/user/login';
    const { loginUsername, loginPassword } = this.data;
    wx.request({
      url: url,
      method: 'POST',
      data: {
        USER_NAME: loginUsername,
        USER_PASSWORD: loginPassword
      },
      header: {
        'content-type': 'application/json'
      },
      success: (res) => {
        if (res.data.status === 200) {
          wx.showToast({
            title: '登录成功',
            icon: 'success'
          });
          // 存储用户登录信息
          wx.setStorageSync('loginUsername', loginUsername);
          wx.setStorageSync('loginPassword', loginPassword);
        } else {
          wx.showToast({
            title: '登录失败: ' + res.data.message,
            icon: 'none'
          });
        }
      },
      fail: (err) => {
        wx.showToast({
          title: '请求失败: ' + err.errMsg,
          icon: 'none'
        });
      }
    });
  },

  // 根据账号密码修改密码接口调用
  updatePassword: function () {
    const baseUrl = 'http://39.104.24.231:8082';
    const url = baseUrl + '/user/updatePw';
    const { updatePwUsername, updatePwOldPassword, updatePwNewPassword } = this.data;
    wx.request({
      url: url,
      method: 'POST',
      data: {
        USER_NAME: updatePwUsername,
        USER_PASSWORD: updatePwOldPassword,
        USER_NEW_PASSWORD: updatePwNewPassword
      },
      header: {
        'content-type': 'application/json'
      },
      success: (res) => {
        if (res.data.status === 200) {
          wx.showToast({
            title: '密码修改成功',
            icon: 'success'
          });
        } else {
          wx.showToast({
            title: '密码修改失败: ' + res.data.message,
            icon: 'none'
          });
        }
      },
      fail: (err) => {
        wx.showToast({
          title: '请求失败: ' + err.errMsg,
          icon: 'none'
        });
      }
    });
  },

  // 根据账号救援码修改密码接口调用
  updatePasswordByKey: function () {
    const baseUrl = 'http://39.104.24.231:8082';
    const url = baseUrl + '/user/updatePwByKey';
    const { updatePwByKeyUsername, updatePwByKeyKey, updatePwByKeyNewPassword } = this.data;
    wx.request({
      url: url,
      method: 'POST',
      data: {
        USER_NAME: updatePwByKeyUsername,
        USER_KEY: updatePwByKeyKey,
        USER_NEW_PASSWORD: updatePwByKeyNewPassword
      },
      header: {
        'content-type': 'application/json'
      },
      success: (res) => {
        if (res.data.status === 200) {
          wx.showToast({
            title: '密码修改成功',
            icon: 'success'
          });
        } else {
          wx.showToast({
            title: '密码修改失败: ' + res.data.message,
            icon: 'none'
          });
        }
      },
      fail: (err) => {
        wx.showToast({
          title: '请求失败: ' + err.errMsg,
          icon: 'none'
        });
      }
    });
  },

  // 删除用户接口调用
  deleteUser: function () {
    const baseUrl = 'http://39.104.24.231:8082';
    const url = baseUrl + '/user/del';
    const { deleteUsername, deletePassword } = this.data;
    wx.request({
      url: url,
      method: 'POST',
      data: {
        USER_NAME: deleteUsername,
        USER_PASSWORD: deletePassword
      },
      header: {
        'content-type': 'application/json'
      },
      success: (res) => {
        if (res.data.status === 200) {
          wx.showToast({
            title: '用户删除成功',
            icon: 'success'
          });
        } else {
          wx.showToast({
            title: '用户删除失败: ' + res.data.message,
            icon: 'none'
          });
        }
      },
      fail: (err) => {
        wx.showToast({
          title: '请求失败: ' + err.errMsg,
          icon: 'none'
        });
      }
    });
  },

  // 页面加载时检查是否有自动登录信息
  onLoad: function () {
    const loginUsername = wx.getStorageSync('loginUsername');
    const loginPassword = wx.getStorageSync('loginPassword');
    if (loginUsername && loginPassword) {
      this.setData({
        loginUsername: loginUsername,
        loginPassword: loginPassword
      });
      this.login();
    }
  }
});